# -*- coding: utf-8 -*-
"""
Created on Tue Feb 04 18:24:31 2014

@author: John Edwardson
"""
import ConfigParser as CP


class UnitConverter(object):
    '''
    A simple class to perform automatic unit conversion.  Conversion factors are 
    are defined in a configuration file(default is units.cfg).  This file has a
    separate section for each type of unit(e.g. radius or energy).  Each section
    specifies a base unit, and factors for converting other units to that base
    '''
    def __init__(self, ufile = 'units.cfg'):        
        self.conversion_factors = {}         
        self.setUnitConfig(ufile)
       
    def setUnitConfig(self, unitFile):
        self.parser = CP.ConfigParser()
        self.parser.optionxform = str
        self.parser.read(unitFile)
                
    def getUnits(self):
        results = {}
        for t in self.getTypes():
            results[t] = []
            for u in self.parser.options(t):
                if u == 'Base':
                    results[t].append(self.parser.get(t, u))
                else:                        
                    results[t].append(u)                
        return results
        
    def getTypes(self):
        return self.parser.sections()
             
    def buildConversionTable(self):      
        #Loop over the unit types
        for t in self.getTypes():
            #Get list of units and base unit
            units = self.parser.options(t)                    
            base = self.parser.get(t, 'Base')
            self.conversion_factors[t] = {}
            for u in units:
                row = {}                    
                if u == 'Base':
                    u = base
                    for v in units:
                        if v == 'Base':
                            row[base] = 1.0 
                        else:
                            row[v] = 1 / self.parser.getfloat(t, v) 
                else:
                    for v in units:
                        if v == 'Base':
                            row[base] = self.parser.getfloat(t, u)
                        else:
                            row[v] = self.parser.getfloat(t, u) * 1 / self.parser.getfloat(t, v)
                self.conversion_factors[t][u] = row
        
    def unit_conversion(self, utype, inUnit, outUnit):
        '''Gets conversion factor for quantity(Radius, Energy, or Scale) with
        input units inkey and output units outkey'''
        return self.conversion_factors[utype][inUnit][outUnit]

'''
Allows user to specify for different energy scales(atomic or molecular) for input and output
'''
class ScaleConverter(object):
    def __init__(self, groundStateEnergy):
        self.gse = groundStateEnergy        
        self.offset = {'Atomic': {'Atomic': 0, 'Molecular': self.ground_state }, 
                      'Molecular': {'Atomic': -self.ground_state , 'Molecular': 0} }       
    
    def unit_conversion(self, inkey, outkey):
        return self.offset[inkey][outkey]        
        
            
            
if __name__ == '__main__':
    converter = UnitConverter()
    print converter.getUnits()
    converter.buildConversionTable()
    print converter.conversion_factors
    